package com.usabb.controller;


import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.usabb.domain.LabelDO;
import com.usabb.service.LabelService;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 标签 and 服务承诺
 * </pre>
 * <small> 2019-08-19 18:03:27 | Aron</small>
 */
@Controller
@RequestMapping("/usabb/label")
public class LabelController extends BaseController {
	@Autowired
	private LabelService labelService;
	
	@GetMapping()
	@RequiresPermissions("usabb:label:label")
	String Label(){
	    return "usabb/label/label";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("usabb:label:label")
	public Result<Page<LabelDO>> list(LabelDO labelDTO){
        Wrapper<LabelDO> wrapper = new EntityWrapper<>(labelDTO).orderBy("id", false);
        Page<LabelDO> page = labelService.selectPage(getPage(LabelDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("usabb:label:add")
	String add(){
	    return "usabb/label/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("usabb:label:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LabelDO label = labelService.selectById(id);
		model.addAttribute("label", label);
	    return "usabb/label/edit";
	}
	
	@Log("添加标签 and 服务承诺")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("usabb:label:add")
	public Result<String> save( LabelDO label){
		labelService.insert(label);
        return Result.ok();
	}
	
	@Log("修改标签 and 服务承诺")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("usabb:label:edit")
	public Result<String>  update( LabelDO label){
		boolean update = labelService.updateById(label);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除标签 and 服务承诺")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("usabb:label:remove")
	public Result<String>  remove( Long id){
		labelService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除标签 and 服务承诺")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("usabb:label:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		labelService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
